﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using HZ.MBSM.DBUtility;
using System.Data.SqlClient;

public partial class Agency_Product_Yaoli : System.Web.UI.Page {

    public string name, medicinemodel, outlookc, strConditions;
    public string show;
    private const int _firstEditCellIndex = 2;
    protected void Page_Load(object sender, EventArgs e) {
        string ProductId, strSql;
        int rowcount = 0;

        if (HZ.MBSM.BLL.Login.CheckAssert(this.UseAccountType)) {
            HZ.MBSM.Model.Account _Account = (HZ.MBSM.Model.Account)System.Web.HttpContext.Current.Session["Account"];
            if (int.Parse(_Account.Type) == 5) {
                this.show = "none";
            } else {
                this.show = "block";
            }
            if (!IsPostBack) {
                Db DbProject = new Db();
                strSql = "SELECT AccountId,AccountName FROM ACCOUNT WHERE TYPE=0 OR TYPE=1 OR  TYPE=5 ";
                System.Data.IDataReader dr = DbProject.DataReader(strSql);
                lstSHR.DataSource = dr;
                lstSHR.Items.Add("全部");
                while (dr.Read()) {
                    lstSHR.Items.Add(dr["AccountName"].ToString());
                }
                dr.Close();
                dr.Dispose();
                txtname.Text = "";
                txtmedicinemodel.Text = "";
                txtoutlook.Text = "";
                DbProject.Close();

                Db DbPro= new Db();
                strSql = "select code as 代号,[Name] as 药理名称 from dbo.Product_Yaoli where layer=2  ";
                DataTable dt = DbPro.DataTable(strSql);
                dt.Rows.Add("-1", "请选择");
               
                lstYaoLi.DataSource= dt;
                lstYaoLi.DataTextField = "药理名称";
                lstYaoLi.DataValueField = "代号";
                lstYaoLi.DataBind();
                lstYaoLi.SelectedValue = "-1";
                dt = null;
                DbPro.Close();   
            }
          showGridView();
        }

    }

    protected void Button5_Click(object sender, EventArgs e)
    {

    }

    /// <summary>
    /// 查询总共有多少页
    /// </summary>
    /// <returns></returns>
    /// <summary>
    /// 将数据绑定到GridView1
    /// </summary>
    /// <param name="PageIndex">转向第几页</param>
    public void showGridView()
    {

        string _SelectField = "productid,ProductCode,productname,medicinemodel,outlookc,Unit,xishu,remark,name";
       
        strConditions = fctConditions();
        Db db = new Db();
        DbPage dbPage = new DbPage(db);
        dbPage.Table = "  Product LEFT OUTER JOIN Product_Yaoli ON left(isnull(Productcode,''),4) = CONVERT(varchar(4), code)";                                               //要查询的表明
        dbPage.Key = "productid";                                               //表的主键    
        dbPage.Where = strConditions;                                           //查询条件    
        dbPage.SelectField = _SelectField;//要显示的字段
        dbPage.PageIndex = SysFun.ToInt(Request["CurrentlyPageIndex"]);          //要显示第几页   
        dbPage.PageSize = 10;                                                    //没有显示记录条数   
        dbPage.OrderBy = "productname,productcode,medicinemodel,outlookc,productid";                  //排序字段
        System.Data.IDataReader dr = dbPage.ExecuteReader();
        GridView1.DataSource = dr;
        GridView1.DataBind();       
        dr.Close();
        dr.Dispose();
        dbPage.Db.Close();
        db.Close();
        dr=null;
        db = null;
        this.DbPageFlip1.DbPage = dbPage;//将数据传给控键



    }
    /// <summary>
    /// 生成查询条件
    /// </summary>
    /// <returns></returns>
    public string fctConditions()
    {
        string strtemp = "1=1 and";
        //品目号

        string _strProductCode = txtProductCode.Text.Trim();
         if (_strProductCode != "")
        {
            strtemp = strtemp + " Productcode like '%" + _strProductCode + "%' and";
        }
  
         //流水号Productid 

         string _Productid = txtLSH.Text.ToString().Trim();
         if (_Productid != "")
        {
             strtemp = strtemp + " Productid =" + _Productid + " and";
        }
        
        //审核人

        string _AccountName_SHR = lstSHR.SelectedValue;
        if (_AccountName_SHR != "全部")
        {
            strtemp = strtemp + " AccountName_SHR like '%" + _AccountName_SHR + "%' and";
        }
        //通用名

        string name = txtname.Text.ToString().Trim();
        if (name != "")
        {
            strtemp = strtemp + " (spellingcode  like '%" + name + "%' or ProductName like '%" + name + "%') and";
        }
        //剂型
        string medicinemodel = txtmedicinemodel.Text.ToString().Trim();
        if (medicinemodel != "")
        {
            strtemp = strtemp + " medicinemodel like '%" + medicinemodel + "%' and";
        }
        //规格
        string outlookc = txtoutlook.Text.ToString().Trim();
        if (outlookc != "")
        {
            strtemp = strtemp + " outlookc like '%" + outlookc + "%' and";
        }
       
        //单位
        string strunit=TextUnit.Text.ToString().Trim();
        if (strunit != "")
        {
            strtemp += " unit like '%" + strunit + "%'  and";
        }
       
        if (chkFilter.Checked)
        {
            strtemp += " productid in (select distinct Productid from goods) and";
        }
        if (lstYaoLi.SelectedValue!="-1")//药理或父级都是相应药理编码
        {
            strtemp += "( code='" + lstYaoLi.SelectedValue + "' or parent='" + lstYaoLi.SelectedValue + "')and";
        }
        //转换系数
        String strXS = txtZhuanhuan.Text.ToString().Trim();
        if (strXS != "")
        {
            strtemp += " xishu = '" + strXS + "' and";
        }
        if (strtemp != "")
        {
            strtemp = "  " + strtemp.Substring(0, strtemp.Length - 3);
        }
        
        return strtemp;

    }
    public string fctConditions2() {
        string strtemp = "1=1 and";
        //品目号

        string _strProductCode = txtProductCode.Text.Trim();
        
        if (_strProductCode != "") {
            strtemp = strtemp + " a.Productcode like '%" + _strProductCode + "%' and";
        }
       

        //流水号

        string _GoodsId = txtLSH.Text.ToString().Trim();
        if (_GoodsId != "") {
            strtemp = strtemp + " a.Productid =" + _GoodsId + " and";
        }
       
        //通用名

        string name = txtname.Text.ToString().Trim();
        if (name != "") {
            strtemp = strtemp + " (a.spellingcode  like '%" + name + "%' or a.ProductName like '%" + name + "%') and";
        }
        //剂型
        string medicinemodel = txtmedicinemodel.Text.ToString().Trim();
        if (medicinemodel != "") {
            strtemp = strtemp + " a.medicinemodel like '%" + medicinemodel + "%' and";
        }
        //规格
        string outlookc = txtoutlook.Text.ToString().Trim();
        if (outlookc != "") {
            strtemp = strtemp + " a.outlookc like '%" + outlookc + "%' and";
        }
      
        //单位
        string strunit = TextUnit.Text.ToString().Trim();
        if (strunit != "") {
            strtemp += " a.unit like '%" + strunit + "%'  and";
        }
       
        //转换系数
        String strXS = txtZhuanhuan.Text.ToString().Trim();
        if (strXS != "") {
            strtemp += " a.xishu = '" + strXS + "' and";
        }
        if (strtemp != "") {
            strtemp = "  " + strtemp.Substring(0, strtemp.Length - 3);
        }

        return strtemp;

    }
    protected void ToEcxul_Click(object sender, EventArgs e) {

        Db db = new Db();
        System.Data.IDataReader dr;

        string _strSql = "Select productid as 目录ID,ProductCode as 品目号,productname as 通用名,medicinemodel as 剂型,outlookc as 规格,Unit as 单位,xishu as 转换系数,name as 药理名称,zxcg as 目录类别,remark as 备注 From   Product LEFT OUTER JOIN Product_Yaoli ON left(isnull(Productcode,''),4) = CONVERT(varchar(4), code)  Where " + fctConditions() + " Order By productname,productcode,medicinemodel,outlookc,productid";
        dr = db.DataReader(_strSql);
        GridView2.DataSource = dr;
        GridView2.DataBind();
        db.Close();
        dr.Close();
        dr.Dispose();
        SysFun.WriteToExcel(GridView2, this.Page, "目录信息.xls");

    }
    protected void ToEcxul2_Click(object sender, EventArgs e) {

        Db db = new Db();
        System.Data.IDataReader dr;
        string _strSql = "Select a.goodsid as 流水号,a.Productid as 目录编号,a.Productcode as 品目名称,a.productname as 通用名,a.GoodsName as 商品名,a.ProductNameSM as 说明书名称,a.licensenumber as 批准文号,a.medicinemodel as 剂型,a.outlookc as 规格,a.factor as 转换系数,case a.QualityLevels " + ConfigurationManager.AppSettings["QualityLevels"].ToString() + " end as 质量层次 ,a.UserCode as 企业注册号 ,a.companyName as 生产企业,a.AccountName_TJR as 添加人,case a.CheckStatus when'0' then '未审核' when null then '未审核' when '1' then '审核通过' when '2' then '不通过' when '3' then '原始状态'  end as 审核状态, a.AccountName_SHR as 审核人,a.shenhetime as 审核时间 ,ISNULL(b.memo5,'') AS 联系人,ISNULL(b.Telepone,'') AS 电话 ,ISNULL(b.memo10,'') AS 手机 From   v_product_goods  as a LEFT OUTER JOIN Account ON a.AccountId_SH = Account.AccountId LEFT OUTER JOIN v_Company AS b on b.UserCode=a.usercode  Where " + fctConditions2() + " Order By a.productname,a.medicinemodel,a.outlookc,a.companyName,a.goodsid";
        dr = db.DataReader(_strSql);
        GridView2.DataSource = dr;
        GridView2.DataBind();
        db.Close();
        dr.Close();
        dr.Dispose();
        SysFun.WriteToExcel(GridView2, this.Page, "商品信息.xls");

    }
    public override void VerifyRenderingInServerForm(Control control)
    {
        //OverRide　为了使导出成Excel可行！

    }
    /// <summary>
    /// 将DataReader 转为 DataTable
    /// </summary>
    /// <param name="DataReader">DataReader</param>
  

    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e) {
        GridView1.EditIndex = e.NewEditIndex;
      showGridView();
    }
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e) {

        //hidProcode.Text = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text.ToString().Trim();
        hidProcode.Text = ((TextBox)(GridView1.Rows[e.RowIndex].FindControl("Textbox1"))).Text.Trim();
        //hidProcode.Text = e.NewValues;
        Db db = new Db();
        HZ.MBSM.Model.Account _Account = (HZ.MBSM.Model.Account)System.Web.HttpContext.Current.Session["Account"];
        string sqlstr = "update product set productcode=" + SysFun.SqlStr(hidProcode.Text) + ",remark='" + _Account.AccountName + "," + System.DateTime.Now.ToString() + "' where productid=" + SysFun.SqlStr(GridView1.Rows[e.RowIndex].Cells[0].Text.ToString());
        db.Command(sqlstr);
        GridView1.EditIndex = -1;
      showGridView();
    }
    //取消
    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e) {
        GridView1.EditIndex = -1;
        showGridView();
    }
    #region IPageDescribe 成员

    public string Name
    {
        get { return "审核商品信息"; }
    }

    public string Describe
    {
        get { return "审核商品信息"; }
    }

    public string ParentName
    {
        get { return "商品维护"; }
    }

    public System.Collections.Generic.List<HZ.MBSM.DAL.AccountType> UseAccountType
    {
        get
        {
            System.Collections.Generic.List<HZ.MBSM.DAL.AccountType> _AccountType = new System.Collections.Generic.List<HZ.MBSM.DAL.AccountType>();
            _AccountType.Add(HZ.MBSM.DAL.AccountType.enumAdmin);
            _AccountType.Add(HZ.MBSM.DAL.AccountType.enumAttorney);
            _AccountType.Add(HZ.MBSM.DAL.AccountType.enumCheckGoods );
            return _AccountType;
        }
    }
    #endregion

}


